package aiP.A3.Client.GUI.Dashboard;

import aiP.A3.Client.Monitor.Instance;
import aiP.A3.Client.Monitor.Monitor;
import aiP.A3.Client.Monitor.MonitorThread;
import aiP.A3.Image.Imageloader;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: mizus
 * Date: 22.05.14
 * Time: 15:08
 * To change this template use File | Settings | File Templates.
 */
public class Dashboard_Gui extends JFrame {

    private static Dashboard_Gui instance;
    private Monitor monitor;

    private Instance instance_I, instance_II;

    public Dashboard_Gui() {
        init();
    }

    public Dashboard_Gui(Monitor screen) throws InterruptedException {
        init();
        setSwitch_Btn_server_I_alive(false);
        setSwitch_Btn_server_II_alive(false);
        connectToMonitor(screen);
        logMessage("Keine Instanzen da!");
    }


    private void connectToMonitor(Monitor screen) {
        monitor = screen;
    }

    public static Dashboard_Gui getInstance() {
        if (instance == null) instance = new Dashboard_Gui();
        return instance;
    }

    public void setInstance_I(Instance tInstance){
        instance_I = tInstance;
    }

    public void setInstance_II(Instance tInstance){
        instance_II = tInstance;
    }

    public Instance getInstance_I(){
        return instance_I;
    }

    public Instance getInstance_II(){
        return instance_II;
    }

    public void logMessage(String str) {
        myLogArea.append(str + "\n\r");
    }

    public void printCallcounterTotal(int counter){
        anfragenCounter.setText("Anzahl der Anfragen insgesamt : " + counter);
    }

    public void setSwitch_Btn_server_I_alive(boolean on) {
        if (on)  {
            lbl_image_server_I.setIcon(Imageloader.getInstance().loadImage(Imageloader.GREEN));
            switch_Btn_server_I.setSelected(true);
        }else{
            switch_Btn_server_I.setSelected(false);
            lbl_image_server_I.setIcon(Imageloader.getInstance().loadImage(Imageloader.RED));
        }
    }

    public void setSwitch_Btn_server_II_alive(boolean on) {
        if (on){
            lbl_image_server_II.setIcon(Imageloader.getInstance().loadImage(Imageloader.GREEN));
            switch_Btn_server_II.setSelected(true);
        } else  {
            lbl_image_server_II.setIcon(Imageloader.getInstance().loadImage(Imageloader.RED));
            switch_Btn_server_II.setSelected(false);
        }
    }


    private void init() {
        this.setTitle("Dashboard ... AMS");
        action();
        myLogArea.setEditable(false);
        centerPnl.setLayout(null);
        centerPnl.setBorder(new EtchedBorder());
        scrollPane.setBorder(new EtchedBorder());

        scrollPane.setBounds(new Rectangle(10, 10, 350, 300));
        switch_Btn_server_I.setBounds(new Rectangle(370, 10, 120, 21));
        switch_Btn_server_II.setBounds(new Rectangle(370, 50, 120, 21));
        btn_close.setBounds(new Rectangle(10, 360, 120, 21));
        showInstanzStatusTime.setBounds(new Rectangle(150, 360, 120, 21));

        anfragenCounter.setBounds(new Rectangle(10, 280, 250, 100));
        lbl_image_server_I.setBounds(new Rectangle(330, 100, 250, 100));
        lbl_image_server_II.setBounds(new Rectangle(330, 250, 250, 100));

        scrollPane.getViewport().setView(myLogArea);
        centerPnl.add(scrollPane, null);
        centerPnl.add(switch_Btn_server_I, null);
        centerPnl.add(switch_Btn_server_II, null);
        centerPnl.add(btn_close);
        centerPnl.add(showInstanzStatusTime);
        centerPnl.add(lbl_image_server_I, null);
        centerPnl.add(lbl_image_server_II, null);
        centerPnl.add(anfragenCounter);

        this.getContentPane().add(centerPnl, BorderLayout.CENTER);
        this.setSize(550, 450);
        this.setVisible(true);
    }


    private void action() {
        btn_close.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        showInstanzStatusTime.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                monitor.printInstanzStatusOnlineTime();
            }
        });

        switch_Btn_server_II.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    switch_action(switch_Btn_server_II, 2);
                } catch (RemoteException e1) {
                    e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                } catch (NotBoundException e1) {
                    e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            }
        });

        switch_Btn_server_I.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    switch_action(switch_Btn_server_I, 1);
                } catch (RemoteException e1) {
                    e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                } catch (NotBoundException e1) {
                    e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            }
        });
    }

    private final int SERVER_INSTANCE_I = 1;
    private final int SERVER_INSTANCE_II = 2;

    private void switch_action(JToggleButton switch_btn_server_i, int Server) throws RemoteException, NotBoundException {
        if(monitor.getInstanzenMap().size() == 0)
        {
            // wenn keine Instanz vorhanden, dann soll Button auch nicht Zustand wechseln
            logMessage("Keine aktiven Instanzen vorhanden.");


           System.out.println("LebendeInstanzen größe:" + monitor.getLebendeInstazen().size());
        }
            if (switch_btn_server_i.isSelected()) {
                switch (Server) {
                    case SERVER_INSTANCE_I:
                        monitor.awake(instance_I.getId());
                        lbl_image_server_I.setIcon(Imageloader.getInstance().loadImage(Imageloader.GREEN));
                        validate();
                        repaint();
                        break;
                    case SERVER_INSTANCE_II:
                        monitor.awake(instance_II.getId());
                        lbl_image_server_II.setIcon(Imageloader.getInstance().loadImage(Imageloader.GREEN));
                        validate();
                        repaint();
                        break;
                }
            } else {
                switch (Server) {
                    case SERVER_INSTANCE_I:
                        monitor.shutDown(instance_I.getId());
                        lbl_image_server_I.setIcon(Imageloader.getInstance().loadImage(Imageloader.RED));
                        validate();
                        repaint();
                        break;
                    case SERVER_INSTANCE_II:
                        monitor.shutDown(instance_II.getId());
                        lbl_image_server_II.setIcon(Imageloader.getInstance().loadImage(Imageloader.RED));
                        validate();
                        repaint();
                        break;
                }
        }
    }

    public synchronized void chanceStatus(boolean isAlive, int index){
        if (isAlive){
            switch (index){
                case 1:
                    lbl_image_server_I.setIcon(Imageloader.getInstance().loadImage(Imageloader.GREEN));
                    switch_Btn_server_I.setSelected(isAlive);
                    break;
                case 2:
                    lbl_image_server_II.setIcon(Imageloader.getInstance().loadImage(Imageloader.GREEN));
                    switch_Btn_server_II.setSelected(isAlive);
                break;
                default:

                    break;
            }
        } else {
            switch (index){
                case 1:
                    lbl_image_server_I.setIcon(Imageloader.getInstance().loadImage(Imageloader.RED));
                    break;
                case 2:
                    lbl_image_server_II.setIcon(Imageloader.getInstance().loadImage(Imageloader.RED));
                    break;
                default:

                    break;
            }

        }
    }

    public void setRedIcon(){

    }


    //////////////////////////////


    private JToggleButton switch_Btn_server_I = new JToggleButton("Server I on/off", true);
    private JToggleButton switch_Btn_server_II = new JToggleButton("Server II on/off", true);

    private JButton btn_close = new JButton("Exit");
    private JButton showInstanzStatusTime = new JButton("Status-Zeiten");

    private JLabel lbl_image_server_II= new JLabel("Server II", JLabel.CENTER);
    private JLabel lbl_image_server_I = new JLabel("Server I", JLabel.CENTER);
    private JLabel anfragenCounter = new JLabel("Anzahl der Anfragen insgesamt : ");

    private JScrollPane scrollPane = new JScrollPane();

    JTextArea myLogArea = new JTextArea();

    JPanel centerPnl = new JPanel();
}
